package com.xtkj.superclub_shop.controller;

import com.xtkj.commons.*;
import com.xtkj.superclub_entity.entity.shop.Address;
import com.xtkj.superclub_entity.entity.shop.Order;
import com.xtkj.superclub_entity.entity.shop.ProItem;
import com.xtkj.superclub_entity.entity.user.User;
import com.xtkj.superclub_shop.client.IUserClient;
import com.xtkj.superclub_shop.service.ICartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author lizheqi
 * @create 2019-12-28-11:54
 */
@RestController
@RequestMapping("/cart")
public class CartController {

    @Autowired
    private SnowflakeIdWorker snowflakeIdWorker;

    @Autowired
    private ICartService cartService;

    @Autowired
    private IUserClient userClient;
    /**
     * 将顾客购买的商品信息添加到购物车
     * @param proItem
     * @param request
     * @return
     */
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public JsonResult addProToCart(@RequestBody ProItem proItem, HttpServletRequest request){
        try {
            //通过请求头拿到用户的userId
            System.out.println(request.getHeader("token")+"-----------");
            String userId = TokenUtil.getUserId(request.getHeader("token"));
            proItem.setUserId(userId);
            //通过雪花生成proItemId
            String proItemId = snowflakeIdWorker.nextId()+"";
            proItem.setProItemId(proItemId);
            cartService.addProToCart(proItem);
            return JsonResult.success("添加到购物车成功").setCode(200);
        }catch (Exception e){
            e.printStackTrace();
            return JsonResult.error(e.getMessage()).setCode(400);
        }
    }

    /**
     * 显示购物车里面的信息
     * @param request
     * @return
     */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public JsonResult shopCartListInfo(HttpServletRequest request){
        String userId = TokenUtil.getUserId(request.getHeader("token"));
        System.out.println(userId+"来了");
        try {
            List<ProItem> proItems = cartService.shopCartListInfo(userId);
            return JsonResult.success("能显示购物车的信息").setData(proItems).setCode(200);
        }catch (Exception e){
            e.printStackTrace();
            return JsonResult.error(e.getMessage()).setCode(400);
        }
    }

    /**
     * 删除购物车的商品
     * @param proItem
     * @param request
     * @return
     */
    @RequestMapping(value = "/del",method = RequestMethod.DELETE)
    public JsonResult deleteShopCartCommodity(@RequestBody ProItem proItem,HttpServletRequest request){
        //通过请求头拿到用户的userId
        String userId = TokenUtil.getUserId(request.getHeader("token"));
        proItem.setUserId(userId);
        try {
            cartService.deleteShopCartCommodity(proItem);
            return JsonResult.success("删除购物车里面的商品成功").setCode(200);
        }catch (Exception e){
            e.printStackTrace();
            return JsonResult.error(e.getMessage()).setCode(400);
        }
    }


    /**
     * 清空购物车
     * @param request
     * @return
     */
    @RequestMapping(value = "/clean",method = RequestMethod.DELETE)
    public JsonResult cleanShopCartList(HttpServletRequest request){
        //通过请求头拿到用户的userId
        String userId = TokenUtil.getUserId(request.getHeader("token"));
        try {
            cartService.cleanShopCartList(userId);
            return JsonResult.success("清空购物车成功").setCode(200);
        }catch (Exception e){
            e.printStackTrace();
            return JsonResult.error(e.getMessage()).setCode(400);
        }
    }

    /**
     * 结算购物车里面的商品
     * @param request
     * @return
     */
    @RequestMapping(value = "/checkout",method = RequestMethod.DELETE)
    public JsonResult checkoutShopCart(HttpServletRequest request, @RequestBody Address address){
        //通过请求头拿到用户的userId
        String userId = TokenUtil.getUserId(request.getHeader("token"));
        try {
            User user = new User();
            user.setUserId(userId);
            JsonResult json = userClient.getUser(user);
            User dbUser = JsonParse.string2Obj(json.getData().toString(),User.class);
            //User user = (User)json.getData();
            cartService.checkoutShopCart(dbUser,address);
            return JsonResult.success("结算购物车成功").setCode(200);
        }catch (Exception e){
            e.printStackTrace();
            return JsonResult.error(e.getMessage()).setCode(400);
        }
    }

    /**
     * 支付完成后操作
     * @param request
     * @param order
     * @return
     */
    @RequestMapping(value = "/pay",method = RequestMethod.PUT)
    public JsonResult pay(HttpServletRequest request,@RequestBody Order order){
        String userId = TokenUtil.getUserId(request.getHeader("token"));
        try {
            cartService.pay(userId,order);
            return JsonResult.success("支付成功").setCode(200);
        }catch (Exception e){
            e.printStackTrace();
            return JsonResult.error(e.getMessage()).setCode(400);
        }
    }
}
